// Package charconv 用于转换字符的编码格式，例如GBK->UTF8
package charconv

import (
	"bytes"
	"io"

	"gitee.com/sillyman/simpleUtil/common/empty"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
)

type GBKConv empty.T

// ToUTF8 将字符编码由 GBK 转为 UTF-8
func (c GBKConv) ToUTF8(r io.Reader) io.Reader {
	return transform.NewReader(r, simplifiedchinese.GBK.NewDecoder())
}

func (c GBKConv) BytesToUTF8(data []byte) []byte {
	r := c.ToUTF8(bytes.NewReader(data))
	newData, _ := io.ReadAll(r)
	return newData
}

func (c GBKConv) StrToUTF8(data string) string {
	return string(c.BytesToUTF8([]byte(data)))
}

// ToGBK 将字符编码由 UTF-8 转为 GBK
func (c GBKConv) ToGBK(r io.Reader) io.Reader {
	return transform.NewReader(r, simplifiedchinese.GBK.NewEncoder())
}

func (c GBKConv) BytesToGBK(data []byte) []byte {
	r := c.ToGBK(bytes.NewReader(data))
	newData, _ := io.ReadAll(r)
	return newData
}

func (c GBKConv) StrToGBK(data string) string {
	return string(c.BytesToGBK([]byte(data)))
}
